package com.yupi.mianshiya.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.yupi.mianshiya.constant.CommonConstant;
import com.yupi.mianshiya.model.dto.announcement.AnnouncementQueryRequest;
import com.yupi.mianshiya.model.entity.Announcement;
import com.yupi.mianshiya.service.AnnouncementService;
import com.yupi.mianshiya.mapper.AnnouncementMapper;
import com.yupi.mianshiya.utils.SqlUtils;
import org.springframework.stereotype.Service;


@Service
public class AnnouncementServiceImpl extends ServiceImpl<AnnouncementMapper, Announcement>
    implements AnnouncementService{

    @Override
    public QueryWrapper<Announcement> getQueryWrapper(AnnouncementQueryRequest announcementQueryRequest) {
        QueryWrapper<Announcement> queryWrapper = new QueryWrapper<>();
        if (announcementQueryRequest == null){
            return queryWrapper;
        }
        Long id = announcementQueryRequest.getId();
        String title = announcementQueryRequest.getTitle();
        Long publisherId = announcementQueryRequest.getPublisherId();
        Integer priority = announcementQueryRequest.getPriority();

        String sortField = announcementQueryRequest.getSortField();
        String sortOrder = announcementQueryRequest.getSortOrder();

        queryWrapper.eq(id != null, "id", id);
        queryWrapper.like(title != null, "title", title);
        queryWrapper.eq(publisherId != null, "publisherId", publisherId);
        queryWrapper.eq(priority != null, "priority", priority);

        // 排序规则
        queryWrapper.orderBy(SqlUtils.validSortField(sortField),
                sortOrder.equals(CommonConstant.SORT_ORDER_ASC),
                sortField);

        return queryWrapper;
    }
}




